﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace ASteroids
{
    class PlayerObject
    {
        public ShotManager PlayerShotManager;
        private HudManager hudManager;
        public long PlayerScore = 0;
        private int playerShield = 25;
        public bool Destroyed = false;
        public Sprite playerSprite;
        public Sprite currWeaponThumbNail;

        private float maxSpeed = 5f;
        private float currentSpeed;
      
        private Vector2 gunOffset = new Vector2(24, 24);
        private float shotTimer = 0.0f;
        private float minShotTimer = 0.3f;      //Denna ska flyttas över till weapon klassen
        private int playerRadius = 15;

        private Keys left;
        private Keys right;
        private Keys up;
        private Keys actionKey;

        private float rotation;
        public Weapon currentWeapon;


        SpriteEffects currSpriteEffect = SpriteEffects.None;
        int tictTop = 8;
        int currentTic = 0;
        float rY;
        float rX;
        bool alreadyShoot = false;
        public bool AutoFire = false;

        private enum PlayerAction { Idle = 0, Rotate, Forward };
        private PlayerAction currentAction;

        public PlayerObject(
            Texture2D texture,
            Rectangle initialFrame,
            int frameCount,
            Rectangle screenBounds, 
            Keys left = Keys.Left, Keys right = Keys.Right, Keys up = Keys.Up, Keys actionKey = Keys.Space)
        {
            currentWeapon = new AutoGun();
            currWeaponThumbNail = new Sprite(new Vector2(7,5),texture,currentWeapon.ThumbNail, Vector2.Zero);
            
            playerSprite = new Sprite(new Vector2(500, 500), texture, initialFrame, Vector2.Zero);
            PlayerShotManager = new ShotManager(texture, currentWeapon.InitialFrame, currentWeapon.FrameCount, currentWeapon.CollisionRadius, currentWeapon.BulletSpeed, screenBounds);
            hudManager = new HudManager(texture, screenBounds, new Vector2(5,5));
            //Controls
            this.left = left;
            this.right = right;
            this.up = up;
            this.actionKey = actionKey;
           
            this.currentSpeed = 0;
            this.rotation = playerSprite.Rotation;

            this.currentAction = PlayerAction.Idle;
            
            for (int x = 1; x < frameCount; x++)
            {
                playerSprite.AddFrame( 
                    new Rectangle(
                        initialFrame.X + (initialFrame.Width * x), 
                        initialFrame.Y, 
                        initialFrame.Width,
                        initialFrame.Height));
            }
            playerSprite.CollisionRadius = playerRadius;
        }

        private void FireShot(Game game)
        {
            if (shotTimer >= currentWeapon.RateOfFire)
            {
                ((Game1)game).PlayCue(currentWeapon.AudioName);
                PlayerShotManager.FireShot(
                    playerSprite.Location + gunOffset,
                    new Vector2(0, -1),playerSprite.Rotation,true);
                shotTimer = 0.0f;
            }
        }

        private void Controls(KeyboardState keyState, Rectangle clientBounds, Game game)
        {
            Vector2 location = playerSprite.Location;

            playerSprite.Velocity = Vector2.Zero;

            currSpriteEffect = SpriteEffects.None;
            


            if (currentTic++ > tictTop)
            {
                if (keyState.IsKeyDown(up))
                {
                    currentSpeed += 0.25f;
                    currentAction = PlayerAction.Forward;

                }
                else
                {
                    currentSpeed -= 0.5f;
                    currentAction = PlayerAction.Idle;
                }

                currentTic = 0;
            }

            if ((keyState.IsKeyDown(actionKey) && !alreadyShoot) || (keyState.IsKeyDown(actionKey) && currentWeapon.AutoFire))
            {
                FireShot(game);
                alreadyShoot = true;
            }
            else if (!keyState.IsKeyDown(actionKey))
                alreadyShoot = false;

            if (keyState.IsKeyDown(left))
            {
                rotation -= 0.1f;
                currentAction = PlayerAction.Rotate;

            }

            if (keyState.IsKeyDown(Keys.M))
            {
                PlayerShield--;

            }

            if (keyState.IsKeyDown(right))
            {
                rotation += 0.1f;
                currSpriteEffect = SpriteEffects.FlipVertically;
                currentAction = PlayerAction.Rotate;
            }

            if (currentSpeed > maxSpeed)
                currentSpeed = maxSpeed;
            else if (currentSpeed < 0)
                currentSpeed = 0;


            rY = (float)Math.Cos(rotation);
            rX = (float) Math.Sin(rotation);
            location += new Vector2(rY, rX) * currentSpeed;

            if (location.X < 0)
                location.X = 0;
            if (location.Y < 0)
                location.Y = 0;
            if (location.X > clientBounds.Width-50)
                location.X = clientBounds.Width - 50;
            if (location.Y > clientBounds.Height - 50)
                location.Y = clientBounds.Height -50;

            playerSprite.Rotation = (float)Math.Atan2(rX, rY);
            playerSprite.Location = location;
        }

        private void imposeMovementLimits()
        {
            Vector2 location = playerSprite.Location;
            
            playerSprite.Location = location;
        }

        public void Update(GameTime gameTime, Rectangle clientBounds, Game game)
        {
            
            PlayerShotManager.Update(gameTime);
            hudManager.Update(gameTime, playerShield);
            shotTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;

            Controls(Keyboard.GetState(), clientBounds, game);
            
            playerSprite.Velocity.Normalize();
            
            playerSprite.Velocity *= maxSpeed;
            
            playerSprite.Update(gameTime);
            
            imposeMovementLimits();
            if (PlayerShield <= 0) {
                Destroyed = true;
            }
            
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            PlayerShotManager.Draw(spriteBatch);
            playerSprite.Draw(spriteBatch);
            currWeaponThumbNail.Draw(spriteBatch);
            hudManager.Draw(spriteBatch);
        }

        public int PlayerShield {
            get { return this.playerShield; }
            set { this.playerShield = value; }
        }
    }
}
